package pagePrint;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.jsp.JspWriter;

import task.Task;
import task.TaskState;
import usr.Usr;
import channel.Channel;
import database.Database;

public class pagePrintTask {
	
	public static void PrintTask(JspWriter out, Usr usr) throws SQLException
	{		//print users' tasks in "tasks.jsp"
		try
		{
			Connection conn = Database.getConnection();
			Statement idstmt = conn.createStatement();
			Statement rsThisstmt = conn.createStatement();
			Statement rsThatstmt = conn.createStatement();
			
			ResultSet idset = idstmt.executeQuery(
					"select id from task where usr = '" + usr.getId() + "'"
			);
			

			out.println("<div class='clear'></div>");
			while(idset.next())
			{
				Task task = new Task();
				task.setId(Integer.parseInt(idset.getString("id")));
				task.update();
				if(task.getState() == TaskState.DELETED)
					continue;
				
				Channel channelThis = task.getChannelThis();
				Channel channelThat = task.getChannelThat();
				
				
				ResultSet rsThis = rsThisstmt.executeQuery(
						"select * from channel where id = " + channelThis.getType()
				);
				
				ResultSet rsThat = rsThatstmt.executeQuery(
						"select * from channel where id = " + channelThat.getType()
				);
				
				rsThis.next();
				rsThat.next();
				
				//begin
				out.println("<br>");
				out.println("<div class = 'task'>");
				//left
				out.println("<div style='float:left'>");
				out.println("<strong class='task'>if</strong>");
				out.print("<img src='" + rsThis.getString("pic_url") + "' ");
				out.println("title='" + rsThis.getString("tag") + "'>");
				out.println("<strong class='task'>then</strong>");
				out.print("<img src='" + rsThat.getString("pic_url") + "' ");
				out.println("title='" + rsThat.getString("tag") + "'>");
				out.println("</div>");
				//right
				out.println("<div style='float:right'>");
				out.println("<br><br>");
				out.println("<a href = 'servTaskDelete?taskid=" + task.getId() + "'>");
				out.println("<div class='label task' title='Delete task' " +
						"style='background-image:url(img/delete_big.png)'></div>");
				out.println("</a>");
				out.println("<a href = 'servTaskStart?taskid=" + task.getId() + "'>");
				out.print("<div class='label task' title='Turn task on/off' " +
						"style='background-image:url(");
				if(task.getState() == TaskState.RUNNING)
					out.println("img/check_false.png)'></div>");
				else
					out.println("img/check_true.png)'></div>");
				out.println("</a>");
				out.println("<div class='label task' title='description : " + task.getDesc() + "' " +
						"style='background-image:url(img/desc.png)'></div>");
				out.println("<a href = 'editTask.jsp?taskid=" + task.getId() + "'>");
				out.println("<div class='label task' title='Edit Task' " +
						"style='background-image:url(img/recipe_add.png)'></div>");
				out.println("</a>");
				out.println("</div>");
				//end
				out.println("</div>");
			}
			
			out.flush();
			
			idstmt.close();
			rsThisstmt.close();
			rsThatstmt.close();
			conn.close();
		}
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
